mui.previewImage();
var Pic = new Vue({
	el: '.mui-content',
	data: {
		items: user_info.pic,
		showstare: false
	},
	methods: {
		delpic: function(item,index) {
			
			if(this.showstare) {
				var a = [{
					title: "刪除"
				}];
				plus.nativeUI.actionSheet({
					title: "刪除照片",
					cancel: "取消",
					buttons: a
				}, function(b) {
					if(b.index == 1) {
						remove(index,item);
					}
				})
			}
		}
	}
})
var delbutton = mui('#compile')[0];
mui('header').on('tap', '#compile', function() {
	if(delbutton.innerHTML == '编辑') {
		delbutton.innerHTML = '取消';
		Pic.showstare = true;
	} else {
		delbutton.innerHTML = '编辑';
		Pic.showstare = false;
	}

});


mui('.img-jia').on('tap', 'a', function() {
	var a = [{
		title: "拍照"
	}, {
		title: "从相册选择"
	}];
	plus.nativeUI.actionSheet({
		title: "上传照片",
		cancel: "取消",
		buttons: a
	}, function(b) {
		if(b.index == 1) {
			showWaiting('上传中…','../../');
			var cmr = plus.camera.getCamera();
			cmr.captureImage(function(p) {
				plus.io.resolveLocalFileSystemURL(p, function(entry) {
					var localurl = entry.toLocalURL();
					appendFile_T(localurl)
				})
			}, function(error) {})
		}
		if(b.index == 2) {

			plus.gallery.pick(function(path) {
				appendFile_T(path);
			})
		}
	})
})

function appendFile_T(path) {
	showWaiting('上传中…','../../');
	var img = new Image();
	img.src = path;
	img.onload = function() {
		var w = this.width,
			h = this.height,
			scale = w / h;
		if(w > 600) {
			w = 600;
		}
		h = w / scale;
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');
		$(canvas).attr({
			width: w,
			height: h
		});
		ctx.drawImage(this, 0, 0, w, h);
		var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8);
		upload(base64)
	}
}

function upload(base64) {
	var url = getApiUrl('http://www.jiaoyou0.cn', 'index/uploadB64');
	mui.ajax({
		type: "post",
		url: url,
		async: true,
		data: {
			'fl': 1,
			'image': base64
		},
		success: function(data) {
			plus.nativeUI.closeWaiting();
			if(data.code == 200) {
				var picnew = {
					"id": data.pid,
					"status": 1,
					"url": data.data,
					"audit": 1
				};
				user_info.pic.push(picnew);
				plus.storage.setItem('user_info', JSON.stringify(user_info));
			}
		},
		error: function(xhr, type, errorThrown) {
			plus.nativeUI.closeWaiting();
			mui.alert('上传失败，请重试！','提示')
		}
	})
}

function remove(index,picid) {
//	plus.nativeUI.showWaiting('正在删除');
	var durl = getApiUrl('http://www.jiaoyou0.cn', 'user/delPic/');
	mui.ajax({
		type: "post",
		data: {
			id: picid
		},
		url: durl,
		async: true,
		success: function(data) {
			if(data.code == 200) {
				user_info.pic.splice(index,1);
				plus.storage.setItem('user_info',JSON.stringify(user_info));
			}
		},
		error:function(){
			mui.toast('请重试！');
		}
	});
}